package com.gch.programa.managed;

import com.gch.programa.util.GCH;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author DanJoas
 */
@ManagedBean(name = "loginBean")
@SessionScoped
public class LoginBean {

    private Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
    private ResourceBundle rb;
    private FacesMessage fm;
    private String GCH_SESSION;
    private String usuario;
    private String clave;
    private String reclave;

    public LoginBean() {
        rb = ResourceBundle.getBundle("com.gch.programa.i18n.global-messages",locale);
    }

    public String nuevo() {
        return "/view/login/nuevo";
    }

    public String registrar() {
        fm = new FacesMessage(rb.getString("gch_login_exito_registrar"));
        FacesContext.getCurrentInstance().addMessage(null, fm);
        return null;
    }

    public String acceso() {
        return "/view/login/acceso";
    }

    public String ingresar() {

        if (getUsuario().equals("danjoas") && getClave().equals("123")) {
            GCH_SESSION = GCH.SESION_USUARIO;
            return "/view/programa/index";
        } else {
            GCH_SESSION = null;
            fm = new FacesMessage(rb.getString("gch_login_error_login"));
            FacesContext.getCurrentInstance().addMessage(null, fm);
            return "/view/login/acceso";
        }
    }

    public String logout() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        if (session != null) {
            session.invalidate();
        }
        return this.acceso();
    }

    public String gch_lenguaje(String lenguaje) {
        GCH.dump("lenguaje", lenguaje);
        locale = new Locale(lenguaje);
        return null;
    }

    // Metodos de acceso de los JavaBeans.
    public String getUsuario() {
        return usuario;
    }

    public void setUsuario(String usuario) {
        this.usuario = usuario;
    }

    public String getClave() {
        return clave;
    }

    public void setClave(String clave) {
        this.clave = clave;
    }

    public String getReclave() {
        return reclave;
    }

    public void setReclave(String reclave) {
        this.reclave = reclave;
    }

    public String getGCH_SESSION() {
        return GCH_SESSION;
    }

    public void setGCH_SESSION(String GCH_SESSION) {
        this.GCH_SESSION = GCH_SESSION;
    }

    public Locale getLocale() {
        return locale;
    }
}
